package linkedlist.pro;

import linkedlist.ListNode;

/**
 * @Classname : IntersectionOfTwoLinkedLists
 * @Description :
 * <a href="https://leetcode.cn/problems/intersection-of-two-linked-lists/">160. 相交链表</a>
 * @Author : chentianyu
 * @Date 2022/12/25 21:21
 */


public class IntersectionOfTwoLinkedLists {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA, b = headB;
        while (a != b) {
            // 在都交换过起点后，要么在交点相遇，要么在各自的尾部相遇（此时都为null）
            a = (a == null ? a = headB : a.next);
            b = (b == null ? b = headA : b.next);
        }
        return a;
    }
}
